package com.dkha.server.controllers;

import com.dkha.common.exception.DkException;
import com.dkha.common.fileupload.MinioUtil;
import com.dkha.common.modules.vo.cut.CutVo;
import com.dkha.common.modules.vo.upload.ImageUploadResult;
import com.dkha.common.result.CommonResult;
import com.dkha.common.systemcode.SystemCode;
import com.dkha.common.util.UtilValidate;
import com.dkha.server.services.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author Spring
 * @Since 2019/12/11 15:24
 * @Description 文件相关Controller -- 没有业务约束的，直接文件上传的，可使用该Controller内部的方法
 */
@RestController
@RequestMapping(value = "/file")
@Api(tags = "文件相关")
@Slf4j
public class FileController extends CommonResult{

    @Autowired
    private IFileService fileService;
    @Value("$(api.picture.type)")
    private String apiPictureType;

    @PostMapping(value = "/picture/upload/multiFaceImages", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "多图/单图上传,其中一张图可以多人脸/单人脸")
    public CommonResult uploadMultiFaceImages(
            @ApiParam(required = true, name = "上传的文件")
            @RequestParam(value = "files")MultipartFile[] multipartFiles) throws Exception {
        if (UtilValidate.isEmpty(multipartFiles)) {
            throw  new DkException("请上传文件");
        }
        ImageUploadResult imageUploadResult = fileService.uploadMultiFaceImages(multipartFiles);
        return successResult(imageUploadResult);
    }

    @PostMapping(value = "/picture/upload/singleFaceImage", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "单图上传,其中一张图只能有一张人脸")
    public CommonResult uploadSingleFaceImage(
            @ApiParam(required = true, name = "上传的文件")
            @RequestParam(value = "files")MultipartFile[] multipartFiles) throws Exception {
        if (UtilValidate.isEmpty(multipartFiles)) {
            throw  new DkException("请上传文件");
        }
        ImageUploadResult imageUploadResult = fileService.uploadSingleFaceImage(multipartFiles);
        return successResult(imageUploadResult);
    }

    @PostMapping(value = "/cut/picture")
    @ApiOperation(value = "裁剪图片")
    public CommonResult cutPicture(
            @ApiParam(required = true, name = "cutVo", value = "")
            @RequestBody CutVo cutVo) {
        try {
            return successResult(fileService.cutPicture(cutVo));
        } catch (Exception e) {
            return failResult(SystemCode.HANDLER_FAILED.des);
        }
    }
}
